package cn.renxuan.repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DynamicParameters {
    private final Map<String, Object> params = new HashMap<>();

    public Map<String, Object> getParams() {
        return this.params;
    }

    public DynamicParameters add(String name, Object value) {
        params.put(name, value);
        return this; // 支持链式调用
    }

    public DynamicParameters addAll(DynamicParameters parameters) {
        for (Map.Entry<String, Object> entry : parameters.params.entrySet()) {
            this.add(entry.getKey(), entry.getValue());
        }
        return this;
    }

    public void applyTo(PreparedStatement stmt) throws SQLException {
        int index = 1;
        for (Object value : params.values()) {
            stmt.setObject(index++, value);
        }
    }
}
